package cn.edu.sjtu.stap.cfg.internal;

import org.aspectj.ajdt.internal.core.builder.AjBuildConfig;

import cn.edu.sjtu.stap.cfg.builder.*;
import cn.edu.sjtu.stap.sdg.builder.*;
import cn.edu.sjtu.stap.cg.builder.*;
import cn.edu.sjtu.stap.hg.builder.*;
import cn.edu.sjtu.stap.output.*;
import cn.edu.sjtu.stap.util.*;

public aspect Initializer {

    before(AjBuildConfig b) : AsmFetcher.asmFetcherPc(b) {
        b.setGenerateModelMode(true);
        initializeBuilders();
        initializeAuxiliaryStructure();
        initializeOutput();
    }

    private static IHgBuilder hgBuilder = null;

    private static ICgBuilder cgBuilder = null;

    private static ICfgBuilder cfgBuilder = null;
    
    private static ISdgBuilder sdgBuilder = null;
    
    public static ResultOutput out = null;

    private static void initializeBuilders() {
        hgBuilder = IHgBuilderFactory.getDefaultHgBuilderFactory().getHgBuilder();
        cgBuilder = ICgBuilderFactory.getDefaultCgBuilderFactory().getCgBuilder();
        cfgBuilder = ICfgBuilderFactory.getDefaultCfgBuilderFactory().getCfgBuilder();
        sdgBuilder = ISdgBuilderFactory.getDefaultSdgBuilderFactory().getSdgBuilder();

        hgBuilder.initialize();
        cgBuilder.initialize();
        cfgBuilder.initialize();
    }

    private static void initializeAuxiliaryStructure() {
        AstAsmMap.getInstance().clear();
        CallMap.getInstance().clear();
    }

    private static void initializeOutput() {
        out = ResultOutput.getInstance();
    }

    public static IHgBuilder getHgBuilder() {
        return hgBuilder;
    }

    public static ICgBuilder getCgBuilder() {
        return cgBuilder;
    }

    public static ICfgBuilder getCfgBuilder() {
        return cfgBuilder;
    }
    
    public static ISdgBuilder getSdgBuilder() {
        return sdgBuilder;
    }
}
